import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class ImageClassifierClient {
    String address;

    ImageClassifierClient () {
        this.address = "http://127.0.0.1";
    }

    ImageClassifierClient (String address) {
        this.address = address;
    }

    String predictImage(int port, String method,String token, String path) {
        StringBuilder json = new StringBuilder();
        try {
            URL url = new URL(this.address + ":" +
                    port + "/" + method + "/" + token + "/" + path);
            URLConnection urlConnection = url.openConnection();
            HttpURLConnection connection = (HttpURLConnection) urlConnection;
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()));
            String line;
            while((line = in.readLine()) != null) {
                json.append(line);
            }
        } catch(IOException e) {
            e.printStackTrace();
        }
        return json.toString();
    }

    public static void main(String[] args) {
        ImageClassifierClient icc = new ImageClassifierClient();
        String json = icc.predictImage(8000, "type",
                "test_token", "test_data/");
        System.out.printf(json);
    }
}